/**
 * Created by CodeTlan.
 * User: Ing. Miguel Salas
 * Date: 3/11/12
 * Time: 4:28 PM
 */
Ext.define('CT.controller.Roles', {
    extend: 'Ext.app.Controller',
    views:[
        'rol.Window',
        'rol.Grid',
        'rol.Form'
    ],
    stores:['Roles' ],
    models:['Rol'],
    win:false,
    init:function(){
        this.control({
            'formrol button[action=add]': {
                click: this.agregar
            },
            'gridrol':{
                cellclick:this.clicked,
                edit:this.editar,
                select:this.btnEnable
            },
            'gridrol button[action=addpermisos]':{
                click:this.agregarPermisos
            },
            'gridrol checkcolumn':{
                checkchange:this.activar
            }
        });
    },
    lanzarVentana:function(anim,icon){
        if(!this.win){
            this.init();
            this.win=Ext.widget('winroles',{animateTarget:anim,iconCls:icon});
        }
        this.win.show();
    },
    clicked:function(grid, cell, index, record){
        switch(index){
            case 3:this.eliminar(grid,record.data);break;
        }
    },
    btnEnable:function(model){
        model.view.panel.down('#btnPermisos').enable();
    },
    agregar:function(button){
        var form = button.up('form').getForm();
        var store=this.getRolesStore();
        if (form.isValid()) {
            form.submit({
                url:'index.php/roles/add',
                success: function() {
                    form.reset();
                    Notification.ok('Rol agregado', ' ');
                    store.load();
                }
            });
        }
    },

    editar:function(editor, e){
        var mods='{"'+e.field+'":"'+e.value+'"}';
        Ext.Ajax.request({
            url: 'index.php/roles/edit',
            params: {
                mod: mods,
                rol_k: e.record.data.rol_k
            },
            success: function(response){
                e.record.commit();
                Notification.ok('Rol Modificado', ' ');
            }
        });
    },

    activar:function(check, index, activo){
        var store = this.getRolesStore();
        var record = store.getAt(index);
        Ext.Ajax.request({
            url: 'index.php/roles/active',
            params: {
                rol_k: record.data.rol_k,
                activo: activo?1:0
            },
            success: function(response){
                record.commit();
                Notification.ok('Rol Modificado', ' ');
            }
        });
    },
    eliminar:function(grid,record){
        Ext.Msg.confirm('Acción requerida','Está seguro que desea eliminar el rol: '+record.titulo+'?',function(r){
            if(r=="yes"){
                var store=this.getRolesStore();
                Ext.Ajax.request({
                    url: 'index.php/roles/delete',
                    params: {
                        rol_k: record.rol_k
                    },
                    success: function(response){
                        var sm = grid.getSelectionModel();
                        store.remove(sm.getSelection());
                        Notification.ok('Rol Eliminado',' ');
                    }
                });
            }
        },this);
    },
    agregarPermisos:function(button){
        var record=button.up("grid").getSelectionModel().getSelection();

        Ext.require('CT.controller.Permisos',function(){
            var controller=this.getController('Permisos');
            controller.lanzarVentana(record[0].data.rol_k);
        },this);
    }
});